//
//  MCQiPaiXiaJiReportCollectionViewCell.m
//  TLYL
//
//  Created by MC on 2017/10/25.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCQiPaiXiaJiReportCollectionViewCell.h"
#import "UIView+MCParentController.h"
#import "MCNaviSelectedPopView.h"
#import <MJRefresh/MJRefresh.h>
#import "MCSignalPickView.h"
#import "MCRecordTool.h"
#import "MCDatePickerView.h"
#import "MCQiPaiXiaJiReportTableViewCell.h"
#import "MCQiPaiXiaJiReportModel.h"
#import "MCQiPaiTeamSlideView.h"
#import "MCCollectionViewFlowLayout.h"
#import "MCQiPaiXiaJiNameCollectionViewCell.h"
#import "MCMineCellModel.h"
#import "MCThirdVRXiaJiReportTableViewCell.h"
#import "MCAGXiaJiReportTableViewCell.h"
#import "MCMineInfoModel.h"

#import "MCThirdVRMyselfReportTableViewCell.h"
#import "MCAGMyselfReportTableViewCell.h"
#import "MCQiPaiMyselfReportTableViewCell.h"

#import "MCQiPaiTeamReportTableViewCell.h"
#import "MCAGTeamReportTableViewCell.h"
#import "MCThirdVRTeamReportTableViewCell.h"
#import "MCQiPaiTeamReportModel.h"

#define MORENCOUNT 15
@interface MCQiPaiXiaJiReportCollectionViewCell()
<
UITableViewDelegate,
UITableViewDataSource,
UICollectionViewDelegate,
UICollectionViewDataSource
>

typedef void(^Compeletion)(BOOL result, NSDictionary *data );

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)ExceptionView * exceptionView;
@property(nonatomic, strong)NSMutableArray * dataMarray;

@property(nonatomic, strong) NSString * statTime;
@property(nonatomic, strong) NSString * endTime;
@property(nonatomic, strong) NSString * CurrentPageIndex;
@property(nonatomic, strong) NSString * CurrentPageSize    ;
@property(nonatomic, assign) BOOL IsHistory;
@property(nonatomic, strong) NSString * GetUserType;//下级类型（0：全部，1：会员，2：代理）
@property(nonatomic, strong) NSString * UserName;//默认传空串，当搜索用户名时传所搜用户名
@property(nonatomic, strong) NSString * User_ID;
@property(nonatomic, strong) NSString * ID ;//   是    Number    第三方平台ID

@property (nonatomic,strong) MCQiPaiXiaJiReportModel * xiaJiReportModel;
@property (nonatomic,strong) MCQiPaiXiaJiReportDataModel * QiPai_Xmodel;
@property (nonatomic,strong) MCThirdVRXiaJiReportDataModel * VR_Xmodel;
@property (nonatomic,strong) MCAGXiaJiReportDataModel  * AG_Xmodel;


@property (nonatomic,strong) MCQiPaiMyselfReportModel * myselfReportModel;
@property (nonatomic,strong) MCQiPaiMyselfReportDataModel * QiPai_Mmodel;
@property (nonatomic,strong) MCThirdVRMyselfReportDataModel * VR_Mmodel;
@property (nonatomic,strong) MCThirdAGMyselfReportDataModel  * AG_Mmodel;


@property (nonatomic,strong) MCQiPaiTeamReportModel * teamReportModel;
@property (nonatomic,strong) MCQiPaiTeamReportDataModel * QiPai_Tmodel;
@property (nonatomic,strong) MCThirdVRTeamTeamReportDataModel * VR_Tmodel;
@property (nonatomic,strong) MCThirdAGTeamTeamReportDataModel * AG_Tmodel;


@property(nonatomic,strong)UICollectionView * firstCollectionView;
@property(nonatomic, strong)NSMutableArray  *  collectionMarry;
@property(nonatomic, strong)NSMutableArray  *  dataCollectionMarry;


@property (nonatomic,assign)BOOL TaskOne;
@property (nonatomic,assign)BOOL TaskTwo;
@property (nonatomic,assign)BOOL TaskThree;

@property (nonatomic,assign)BOOL FinishOne;
@property (nonatomic,assign)BOOL FinishTwo;
@property (nonatomic,assign)BOOL FinishThree;

@property (nonatomic,strong)NSDictionary *data1;
@property (nonatomic,strong)NSDictionary *data2;
@property (nonatomic,strong)NSDictionary *data3;

@end

@implementation MCQiPaiXiaJiReportCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        [self initView];
    }
    return self;
}


- (void)initView{
    
    [self setProperty];
    
    [self createUI];
    
    [self refreashData];
    
}

-(void)relayOutDataCollectionMarry{
    
    
    NSString * UserName=[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];

    
    if ([_ID isEqualToString:@"1"]||[_ID isEqualToString:@"2"]) {
        MCQiPaiXiaJiReportCommModel * mm = [[MCQiPaiXiaJiReportCommModel alloc]init];
        mm.UserName =UserName;
        mm.UserID=[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        [_dataCollectionMarry addObject:mm];
        
      
    }else if ([_ID isEqualToString:@"3"]) {

        MCThirdVRXiaJiReportCommModel * mm = [[MCThirdVRXiaJiReportCommModel alloc]init];
        mm.UserName =UserName;
        mm.UserID=[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        [_dataCollectionMarry addObject:mm];
        
        
        
    }else{
        
        MCAGXiaJiReportCommModel * mm = [[MCAGXiaJiReportCommModel alloc]init];
        mm.UserName =UserName;
        mm.UserID=[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        [_dataCollectionMarry addObject:mm];
    }
    
}

#pragma mark==================setProperty======================
-(void)setProperty{
    
    self.backgroundColor=RGB(231, 231, 231);

    [self refreashQiPaiPersonProperty];
    _dataMarray=[[NSMutableArray alloc]init];
    _collectionMarry = [[NSMutableArray alloc]init];
    _dataCollectionMarry = [[NSMutableArray alloc]init];
    
    NSString * UserName=[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    [_collectionMarry addObject:[NSString stringWithFormat:@"%@ >",UserName]];
    _User_ID =[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    
    [self relayOutDataCollectionMarry];
    
}



-(void)refreashQiPaiPersonProperty{
    MCQiPaiTeamProperty * TeamProperty = [MCQiPaiTeamProperty sharedMCQiPaiTeamProperty];
    self.statTime=TeamProperty.statTime;
    self.endTime=TeamProperty.endTime;
    self.CurrentPageIndex=TeamProperty.CurrentPageIndex;
    self.CurrentPageSize=TeamProperty.CurrentPageSize;
    self.UserName=TeamProperty.UserName;
    self.GetUserType=TeamProperty.GetUserType;
    self.ID=TeamProperty.ID;
    //    self.ID=@"1";
    
    [_collectionMarry removeAllObjects];
    [_dataCollectionMarry removeAllObjects];
    NSString * UserName=[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
    [_collectionMarry addObject:[NSString stringWithFormat:@"%@ >",UserName]];
    _User_ID =[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    
    [self relayOutDataCollectionMarry];
    
    [self.firstCollectionView reloadData];
}

#pragma mark==================createUI======================
-(void)createUI{
    UIView * upView = [[UIView alloc]init];
    upView.backgroundColor=RGB(249,249,249);
    [self addSubview:upView];
    [upView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
        make.height.mas_equalTo(40);
    }];
    
    UIImageView * logoImgV = [[UIImageView alloc]init];
    [upView addSubview:logoImgV];
    [logoImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(12);
        make.left.equalTo(self.mas_left).offset(18);
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(16);
    }];
    logoImgV.image=[UIImage imageNamed:@"qygl-gzjl-xz"];
    
    
    [upView addSubview:self.firstCollectionView];
    [self.firstCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(upView.mas_top).offset(0);
        make.left.equalTo(upView.mas_left).offset(40);
        make.right.equalTo(upView.mas_right).offset(0);
        make.height.mas_equalTo(40);
    }];
    
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.layer.cornerRadius=5;
    _tableView.clipsToBounds=YES;
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreashData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(50);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
    }];
    
}

#pragma mark-下拉刷新
- (void)refreashData{
    
    NSLog(@"加载接口----------3个------------------");
    
    self.FinishOne=NO;
    self.FinishTwo=NO;
    self.FinishThree=NO;
    
    [self.exceptionView dismiss];
    self.exceptionView = nil;
    [_dataMarray removeAllObjects];

    self.tableView.mj_header.hidden=NO;
    self.tableView.mj_footer.hidden=NO;
    [self.exceptionView dismiss];
    self.exceptionView = nil;
    [BKIndicationView showInView:self];
    __weak __typeof__ (self) wself = self;

//    dispatch_group_t group = dispatch_group_create();
//    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
//    dispatch_group_async(group, quene, ^{

    
        [self loadData_XiaJi:^(BOOL result, NSDictionary *data) {
            
            wself.TaskThree=result;
            wself.data3=data;
            wself.FinishThree=YES;
//            dispatch_semaphore_signal(semaphore);
            if (result) {
                NSLog(@"complete task 3-YES");
            }else{
                NSLog(@"complete task 3-NO");
            }
 
            [wself setAllData];
        }];

//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//    });

//    dispatch_group_async(group, quene, ^{
        [self loadData_Self:^(BOOL result, NSDictionary *data) {
           
            wself.TaskOne=result;
            wself.data1=data;
            wself.FinishOne=YES;
           
//            dispatch_semaphore_signal(semaphore);
            if (result) {
                NSLog(@"complete task 1-YES");
            }else{
                NSLog(@"complete task 1-NO");
            }
             [wself setAllData];
        }];
//
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//    });
    
//    dispatch_group_async(group, quene, ^{
        [self loadData_Team:^(BOOL result, NSDictionary *data) {
            
            wself.TaskTwo=result;
            wself.data2=data;
             wself.FinishTwo=YES;
//            dispatch_semaphore_signal(semaphore);
            if (result) {
                NSLog(@"complete task 2-YES");
            }else{
                NSLog(@"complete task 2-NO");
            }
            [wself setAllData];
        }];
//        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
//    });
    
    
//    dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        NSLog(@"完成了网络请求，不管网络请求失败了还是成功了。");
//
//
//    });

}


-(void)setAllData{
    
    
    
    __weak __typeof__ (self) wself = self;
    if (self.FinishOne&&self.FinishTwo&&self.FinishThree) {
        
    }else{
        return;
    }
    
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    
    if (self.TaskOne&&self.TaskTwo&&self.TaskThree) {
        [_dataMarray removeAllObjects];
        [self setData_Self:self.data1];
        [self setData_Team:self.data2];
        [self setData_XiaJi:self.data3];
        [self.tableView reloadData];
        NSLog(@"准备数据ing-------");
    }else{
        self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeRequestFailed];
        self.exceptionView.heightH=G_SCREENHEIGHT-kStatusBarAndNavigationBarHeight-49;
        ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeRequestFailed handler:^(ExceptionViewAction *action) {
            [wself.exceptionView dismiss];
            wself.exceptionView = nil;
            [wself refreashData];
        }];
        [self.exceptionView addAction:action];
        [self.exceptionView showInView:self];
    }
}


-(void)loadMoreData{
    [self.exceptionView dismiss];
    self.exceptionView = nil;
    _CurrentPageIndex=[NSString stringWithFormat:@"%d",[_CurrentPageIndex intValue]+1];
    [BKIndicationView showInView:self];
    __weak __typeof__ (self) wself = self;
    [self loadData_XiaJi:^(BOOL result, NSDictionary *data) {
        [wself.tableView.mj_footer endRefreshing];
        [wself.tableView.mj_header endRefreshing];
        if (result) {
            [wself setData_XiaJi:data];
        }else{
            [SVProgressHUD showErrorWithStatus:@"加载失败"];
            wself.CurrentPageIndex=[NSString stringWithFormat:@"%d",[_CurrentPageIndex intValue]-1];
        }
    }];
}


#pragma mark==================loadData======================
#pragma mark-下级
-(void)loadData_XiaJi:(Compeletion)compeletion{
//    if (_UserName.length>0) {
//        //开启搜索功能   清空关系链
//        _User_ID = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
//
//        [_collectionMarry removeAllObjects];
//        NSString * UserName=[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
//        [_collectionMarry addObject:[NSString stringWithFormat:@"%@ >",UserName]];
//
//        [self.firstCollectionView reloadData];
//
//    }
    NSDictionary *  dic =@{
                           
                           @"ID":self.ID,
                           @"User_ID":_User_ID,
                           @"subUserName":_UserName,
                           @"GetUserType":_GetUserType,
                           @"BeginTime":_statTime,
                           @"EndTime":_endTime,
                           @"CurrentPageIndex":_CurrentPageIndex,
                           @"CurrentPageSize":_CurrentPageSize
                           };
    
    
    MCQiPaiXiaJiReportModel * xiaJiReportModel = [[MCQiPaiXiaJiReportModel alloc]initWithDic:dic];
    [xiaJiReportModel refreashDataAndShow];
    self.xiaJiReportModel = xiaJiReportModel;
    
    xiaJiReportModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        compeletion(NO,nil);
    };
    
    xiaJiReportModel.callBackSuccessBlock = ^(id manager) {
        compeletion(YES,manager);
    };
    
}

-(void)setData_XiaJi:(NSDictionary *)dic{
    
    _QiPai_Xmodel=nil;
    _VR_Xmodel=nil;
    _AG_Xmodel=nil;
    
    if ([_ID isEqualToString:@"1"]||[_ID isEqualToString:@"2"]) {
        _QiPai_Xmodel = [MCQiPaiXiaJiReportDataModel mj_objectWithKeyValues:dic];
        
        [_dataMarray addObjectsFromArray:_QiPai_Xmodel.ReportComm];
        
    }else if([_ID isEqualToString:@"3"]){
        _VR_Xmodel = [MCThirdVRXiaJiReportDataModel mj_objectWithKeyValues:dic];
        
        [_dataMarray addObjectsFromArray:_VR_Xmodel.VRRPtlist];
        
    }else{
        _AG_Xmodel = [MCAGXiaJiReportDataModel mj_objectWithKeyValues:dic];
        
        [_dataMarray addObjectsFromArray:_AG_Xmodel.ReportComm];
    }
    
    
    if (_dataMarray.count<1) {
        //        //无数据
        //        self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
        //        self.exceptionView.heightH=G_SCREENHEIGHT-64-49-40;
        //        [self.exceptionView showInView:self.tableView];
        //        self.tableView.mj_header.hidden=YES;
        //        self.tableView.mj_footer.hidden=YES;
        //        return;
        self.tableView.mj_footer.hidden=YES;
    }
    if (_dataMarray.count%MORENCOUNT!=0) {
        self.tableView.mj_footer.hidden=YES;
    }
    
    [self.tableView reloadData];
}

#pragma mark-自身
-(void)loadData_Self:(Compeletion)compeletion{

//    MCMineInfoModel * mineInfoModel=[MCMineInfoModel sharedMCMineInfoModel];
//    NSString * UserName;
//    if (mineInfoModel.UserName.length>1) {
//        UserName=mineInfoModel.UserName;
//    }else{
//        UserName=[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
//    }
    NSDictionary * dic=@{
                             @"UserName":_UserName,
                             @"ISself":@"1",
                             @"BeginTime":_statTime,
                             @"EndTime":_endTime,
                             @"ID":self.ID,
                             @"subUserName":_UserName,
                             @"UserID":_User_ID
                         };
    
    MCQiPaiMyselfReportModel * myselfReportModel = [[MCQiPaiMyselfReportModel alloc]initWithDic:dic];
    [myselfReportModel refreashDataAndShow];
    self.myselfReportModel = myselfReportModel;
    
    myselfReportModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        compeletion(NO,nil);
    };
    
    myselfReportModel.callBackSuccessBlock = ^(id manager) {
        compeletion(YES,manager);
    };
    
}

-(void)setData_Self:(NSDictionary *)dic{
    
    _QiPai_Mmodel=nil;
    _VR_Mmodel=nil;
    if ([_ID isEqualToString:@"1"]||[_ID isEqualToString:@"2"]) {
        _QiPai_Mmodel=[MCQiPaiMyselfReportDataModel mj_objectWithKeyValues:dic];
    }else if ([_ID isEqualToString:@"3"]) {
        _VR_Mmodel = [MCThirdVRMyselfReportDataModel  mj_objectWithKeyValues:dic];
    }else{
        _AG_Mmodel = [MCThirdAGMyselfReportDataModel  mj_objectWithKeyValues:dic];
    }
    
    //    if (!_QiPai_Mmodel && !_VR_Mmodel &&!_AG_Mmodel) {
    //        //无数据
    //        self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
    //        self.exceptionView.heightH=G_SCREENHEIGHT-64-49-40;
    //        [self.exceptionView showInView:self.tableView];
    //        self.tableView.mj_header.hidden=YES;
    //        return;
    //    }
    [self.tableView reloadData];
}


#pragma mark-团队
-(void)loadData_Team:(Compeletion)compeletion{

//    MCMineInfoModel * mineInfoModel=[MCMineInfoModel sharedMCMineInfoModel];
//    NSString * UserName;
//    if (mineInfoModel.UserName.length>1) {
//        UserName=mineInfoModel.UserName;
//    }else{
//        UserName=[[NSUserDefaults standardUserDefaults] objectForKey:@"UserName"];
//    }

    NSDictionary * dic=@{
                         @"UserName":_UserName,
                         @"ISself":@"0",
                         @"BeginTime":_statTime,
                         @"EndTime":_endTime,
                         @"ID":self.ID,
                         @"subUserName":_UserName,
                         @"UserID":_User_ID
                         };
    
    MCQiPaiTeamReportModel * teamReportModel = [[MCQiPaiTeamReportModel alloc]initWithDic:dic];
    [teamReportModel refreashDataAndShow];
    self.teamReportModel = teamReportModel;
    
    teamReportModel.callBackFailedBlock = ^(ApiBaseManager *manager, NSString *errorCode) {
        
        compeletion(NO,nil);
    };
    
    teamReportModel.callBackSuccessBlock = ^(id manager) {
        compeletion(YES,manager);
        
    };
    
}

-(void)setData_Team:(NSDictionary *)dic{
    
    _QiPai_Tmodel=nil;
    _VR_Tmodel=nil;
    if ([_ID isEqualToString:@"1"]||[_ID isEqualToString:@"2"]) {
        _QiPai_Tmodel=[MCQiPaiTeamReportDataModel mj_objectWithKeyValues:dic];
    }else if ([_ID isEqualToString:@"3"]) {
        _VR_Tmodel = [MCThirdVRTeamTeamReportDataModel  mj_objectWithKeyValues:dic];
    }else{
        _AG_Tmodel = [MCThirdAGTeamTeamReportDataModel  mj_objectWithKeyValues:dic];
    }
    
    if (!_QiPai_Tmodel && !_VR_Tmodel && !_AG_Tmodel) {
//        //无数据
//        self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoData];
//        self.exceptionView.heightH=G_SCREENHEIGHT-64-49-40;
//        [self.exceptionView showInView:self.tableView];
//        self.tableView.mj_header.hidden=YES;
//        return;
    }
    [self.tableView reloadData];
    
}


#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataMarray.count+2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        if ([_ID isEqualToString:@"1"]) {
            
            return [MCQiPaiTeamReportTableViewCell computeHeight:nil];
            
        }else if([_ID isEqualToString:@"2"]){
            return [MCKaiYuanQiPaiTeamReportTableViewCell computeHeight:nil];
        }else if([_ID isEqualToString:@"3"]){
            return [MCThirdVRTeamReportTableViewCell computeHeight:nil];
        }else{
            return [MCAGTeamReportTableViewCell computeHeight:nil];
        }
    }else if (indexPath.row==1){
        
        if ([_ID isEqualToString:@"1"]) {
            return [MCQiPaiMyselfReportTableViewCell computeHeight:nil];
        }else if([_ID isEqualToString:@"2"]){
            return [MCKaiYuanQiPaiMyselfReportTableViewCell computeHeight:nil];
        }else if([_ID isEqualToString:@"3"]){
            return [MCThirdVRMyselfReportTableViewCell computeHeight:nil];
        }else{
            return [MCAGMyselfReportTableViewCell computeHeight:nil];
        }
    }else{
        if ([_ID isEqualToString:@"1"]) {
            return [MCQiPaiXiaJiReportTableViewCell computeHeight:nil];
        }else if([_ID isEqualToString:@"2"]){
            return [MCKaiYuanQiPaiXiaJiReportTableViewCell computeHeight:nil];
        }else if([_ID isEqualToString:@"3"]){
            return [MCThirdVRXiaJiReportTableViewCell computeHeight:nil];
        }else{
            return [MCAGXiaJiReportTableViewCell computeHeight:nil];
        }
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row==0) {
        if ([_ID isEqualToString:@"1"]) {
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCQiPaiTeamReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCQiPaiTeamReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCQiPaiTeamReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_QiPai_Tmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else if([_ID isEqualToString:@"2"]){
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCKaiYuanQiPaiTeamReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCKaiYuanQiPaiTeamReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCKaiYuanQiPaiTeamReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_QiPai_Tmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }else if([_ID isEqualToString:@"3"]){
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCThirdVRTeamReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCThirdVRTeamReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCThirdVRTeamReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_VR_Tmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCAGTeamReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCAGTeamReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCAGTeamReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_AG_Tmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }else if (indexPath.row==1){
        
        if ([_ID isEqualToString:@"1"]) {
            
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCQiPaiMyselfReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCQiPaiMyselfReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCQiPaiMyselfReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_QiPai_Mmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }else if([_ID isEqualToString:@"2"]){
            
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCKaiYuanQiPaiMyselfReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCKaiYuanQiPaiMyselfReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCKaiYuanQiPaiMyselfReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_QiPai_Mmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }else if([_ID isEqualToString:@"3"]){
            
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCThirdVRMyselfReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCThirdVRMyselfReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCThirdVRMyselfReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_VR_Mmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else{
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCAGMyselfReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCAGMyselfReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCAGMyselfReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            cell.dataSource=_AG_Mmodel;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }else{
#pragma mark-下级Cell
        if ([_ID isEqualToString:@"1"]) {
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCQiPaiXiaJiReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCQiPaiXiaJiReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCQiPaiXiaJiReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            if (_dataMarray.count>0) {
                cell.dataSource=_dataMarray[indexPath.row-2];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }else if([_ID isEqualToString:@"2"]){
            
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCKaiYuanQiPaiXiaJiReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCKaiYuanQiPaiXiaJiReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCKaiYuanQiPaiXiaJiReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            if (_dataMarray.count>0) {
                cell.dataSource=_dataMarray[indexPath.row-2];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }else if([_ID isEqualToString:@"3"]){
            
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCThirdVRXiaJiReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCThirdVRXiaJiReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCThirdVRXiaJiReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            
            if (_dataMarray.count>0) {
                cell.dataSource=_dataMarray[indexPath.row-2];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
            
        }else{
            NSString *reuseIdentifier =[NSString stringWithFormat:@"MCAGXiaJiReportTableViewCell-%ld-%ld",(long)indexPath.section,(long)indexPath.row];
            MCAGXiaJiReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
            if (!cell) {
                cell = [[MCAGXiaJiReportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            if (_dataMarray.count>0) {
                cell.dataSource=_dataMarray[indexPath.row-2];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row<2) {
        return;
    }
    
    if (_dataMarray.count<1) {
        return;
    }
    _UserName=@"";
#pragma mark-下级
    if ([_ID isEqualToString:@"1"]||[_ID isEqualToString:@"2"]) {
        
        MCQiPaiXiaJiReportCommModel * model = _dataMarray[indexPath.row-2];
        if ([model.ChildNum integerValue]<1) {
            //        [SVProgressHUD showInfoWithStatus:@"暂无下级！"];
            return;
        }
        
        _User_ID = model.UserID;
        if (_User_ID.length<1) {
            _User_ID =[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        }
        
        self.CurrentPageIndex=@"1";
//        _UserName = model.UserName;
        [self refreashData];
        
        [_collectionMarry addObject:[NSString stringWithFormat:@"%@ >",model.UserName]];
        [_dataCollectionMarry addObject:model];
        [self.firstCollectionView reloadData];
        
        
        
        
    }else if([_ID isEqualToString:@"3"]){
        
        
        MCThirdVRXiaJiReportCommModel * model = _dataMarray[indexPath.row-2];
        if ([model.ChildNum integerValue]<1) {
            //        [SVProgressHUD showInfoWithStatus:@"暂无下级！"];
            return;
        }
        
        _User_ID = model.UserID;
        if (_User_ID.length<1) {
            _User_ID =[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        }
        
        self.CurrentPageIndex=@"1";
//        _UserName = model.UserName;
        [self refreashData];
        
        [_collectionMarry addObject:[NSString stringWithFormat:@"%@ >",model.UserName]];
        [_dataCollectionMarry addObject:model];
        [self.firstCollectionView reloadData];
        
    }else{
        MCAGXiaJiReportCommModel * model = _dataMarray[indexPath.row-2];
        if ([model.ChildNum integerValue]<1) {
            //        [SVProgressHUD showInfoWithStatus:@"暂无下级！"];
            return;
        }
        
        _User_ID = model.UserID;
        if (_User_ID.length<1) {
            _User_ID =[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        }
        
        self.CurrentPageIndex=@"1";
//        _UserName = model.UserName;
//        _UserName=@"";
        [self refreashData];
        
        [_collectionMarry addObject:[NSString stringWithFormat:@"%@ >",model.UserName]];
        [_dataCollectionMarry addObject:model];
        [self.firstCollectionView reloadData];
        
    }
    
}




//第一个开奖期号
-(UICollectionView *)firstCollectionView{
    if (!_firstCollectionView) {
        
        //创建一个layout布局类
        MCCollectionViewFlowLayout * layout = [[MCCollectionViewFlowLayout alloc]init];
        //设置布局方向为横向流布局
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _firstCollectionView=[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _firstCollectionView.backgroundColor=[UIColor clearColor];
        _firstCollectionView.dataSource=self;
        _firstCollectionView.delegate=self;
        [_firstCollectionView registerClass:[MCQiPaiXiaJiNameCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MCQiPaiXiaJiNameCollectionViewCell class])];
        
    }
    return _firstCollectionView;
}



#pragma mark - <UICollectionViewDataSource>
// 设置headerView和footerView的
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        return nil ;
    }
    return nil ;
}

//设置section的颜色
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section{
    return [UIColor clearColor];
}
//设置item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_collectionMarry.count>0) {
        NSString * aString = _collectionMarry[indexPath.row];
        CGFloat width=[aString boundingRectWithSize:CGSizeMake(1000, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size.width +10;
        return CGSizeMake(width, 40);
    }
    return CGSizeMake(0, 0);
}
//设置section的边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
// 两个cell之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
    
}
// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 2;
}

//numberOfItemsInSection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.collectionMarry.count>0) {
        return self.collectionMarry.count;
    }
    return 0;
}

//numberOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


//UICollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MCQiPaiXiaJiNameCollectionViewCell*  cell =[collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MCQiPaiXiaJiNameCollectionViewCell class]) forIndexPath:indexPath];
    if (_collectionMarry.count>0) {
        cell.dataSource=_collectionMarry[indexPath.row];
    }
    return cell;
    
}
#pragma mark-didSelect
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if ((indexPath.row+1)==self.collectionMarry.count) {
        return;
    }
    NSString * name=self.collectionMarry[indexPath.row];
    name=[name stringByReplacingOccurrencesOfString:@" " withString:@""];
    name=[name stringByReplacingOccurrencesOfString:@">" withString:@""];
    _User_ID = @"";
    
    
    if ([_ID isEqualToString:@"1"]||[_ID isEqualToString:@"2"]) {
//        for (MCQiPaiXiaJiReportCommModel * model in _QiPai_Xmodel.ReportComm) {
//            if ([model.UserName isEqualToString:name]) {
//                _User_ID = model.UserID;
//                 _UserName=model.UserName;
//            }
//        }
        for (MCQiPaiXiaJiReportCommModel * model in _dataCollectionMarry) {
            if ([model.UserName isEqualToString:name]) {
                _User_ID = model.UserID;
                _UserName=model.UserName;
            }
        }
    }else if ([_ID isEqualToString:@"3"]) {
        
//        for (MCThirdVRXiaJiReportCommModel * model in _VR_Xmodel.VRRPtlist) {
//            if ([model.UserName isEqualToString:name]) {
//                _User_ID = model.UserID;
//                 _UserName=model.UserName;
//            }
//        }
        
        for (MCThirdVRXiaJiReportCommModel * model in _dataCollectionMarry) {
            if ([model.UserName isEqualToString:name]) {
                _User_ID = model.UserID;
                _UserName=model.UserName;
            }
        }
        
    }else{
        
//        for (MCAGXiaJiReportCommModel * model in _AG_Xmodel.ReportComm) {
//            if ([model.UserName isEqualToString:name]) {
//                _User_ID = model.UserID;
//                _UserName=model.UserName;
//            }
//        }
        
        for (MCAGXiaJiReportCommModel * model in _dataCollectionMarry) {
            if ([model.UserName isEqualToString:name]) {
                _User_ID = model.UserID;
                _UserName=model.UserName;
            }
        }
    }
    
    if (_User_ID.length<1) {
        _User_ID =[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    }
    _UserName = @"";
    [self refreashData];
    
    NSInteger t = _collectionMarry.count - indexPath.row -1;
    if (t>0) {
        for (NSInteger i =0 ; i < t; i++) {
            [_collectionMarry removeLastObject];
        }
    }
    [self.firstCollectionView reloadData];
}

@end

